package com.mxq.eclipse.mqtt.demo.config;

import com.mxq.eclipse.mqtt.demo.subscribe.MqttSubscribeDto;
import com.mxq.eclipse.mqtt.demo.subscribe.service.MqttSubscribeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * MQTT订阅启动
 *
 * @author xiaoqiang
 * @date 2020-05-09 15:52
 */
@Slf4j
@Component
public class MqttRunner implements ApplicationRunner {
    @Value("${mqtt.topic}")
    private String topic;
    @Value("${mqtt.username}")
    private String username;
    @Value("${mqtt.password}")
    private String password;

    @Autowired
    private MqttSubscribeService mqttSubscribeService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String [] topics = topic.split(",");
        if (topics.length < 1){
            topics = new String[] {topic};
        }
        mqttSubscribeService.subscribe(new MqttSubscribeDto(topics, username, password));
        log.info(" MQTT Subscribe Server 启动");
    }
}
